<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Random 8</title>
    <link rel="stylesheet" href="../include/style.css">
  </head>
  <body>
    <header>
      Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
    </header>
    <canvas id="canvas" width="400" height="400"></canvas>
    <aside>Refresh page for random positions.</aside>

    <script src="../include/utils.js"></script>
    <script src="./classes/ball.js"></script>
    <script>
    window.onload = function () {
      var canvas = document.getElementById('canvas'),
          context = canvas.getContext('2d'),
          numDots = 300,
          ball0 = new Ball(),
          ball1 = new Ball(80);

      ball0.x = 100;
      ball0.y = canvas.height / 2;
      ball1.x = 300;
      ball1.y = canvas.height / 2;

      function detectCollision (x, y, ball) {
        var dx = x - ball.x,
            dy = y - ball.y,
            dist = Math.sqrt(dx * dx + dy * dy);
        return (dist < ball.radius); //true or false
      }

      while (numDots--) {
        //initialize variables
        var x = 0,
            y = 0;
        
        //if not in ballA AND not in ballB, set new random position
        while (!detectCollision(x, y, ball0) && !detectCollision(x, y, ball1)) {
          //get random position on canvas
          x = Math.random() * canvas.width;
          y = Math.random() * canvas.height;
        }

        context.fillStyle = "#000000";
        context.beginPath();
        //x, y, radius, start_angle, end_angle, anti-clockwise
        context.arc(x, y, 2, 0, (Math.PI * 2), true);
        context.closePath();
        context.fill();
      }
    };
    </script>
  </body>
</html>
